﻿using Application.MainModule.IdentitiesManagement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Domain.MainModule.Identities;
using Domain.MainModule.Entities;
using System.Collections.Generic;

using Infraestructure.CrossCutting.IoC;

namespace Application.MainModule.Tests
{
    /// <summary>
    ///This is a test class for IdentityManagementServiceTest and is intended
    ///to contain all IdentityManagementServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class IdentityManagementServiceTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for AddIdentity
        ///</summary>
        [TestMethod()]
        public void AddIdentity_Invoke_Test()
        {
            //Arrange
            IIdentityManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();
            Guid identityId = Guid.Parse("ABCDF456-1234-5678-5678-FFFFFFFFFFFF");
            Identity identity = new Identity()
            {
                IdentityId = identityId,
                Value = "value"
            };
            Identity result = null;

            //Act
            target.AddIdentity(identity);

            //Commit
            result = target.FindIdentityById(identityId);
            Assert.IsNotNull(result);
            Assert.AreEqual(identityId, result.IdentityId);
            target.RemoveIdentity(result);
        }

        /// <summary>
        ///A test for ChangeIdentity
        ///</summary>
        [TestMethod()]
        public void ChangeIdentity_Invoke_Test()
        {
            //Arrange
            IIdentityManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();
            Guid identityId = Guid.Parse("ABCDF456-1234-5678-5678-EFAB567890AB");
            Identity identity = null;
            Identity result = null;

            //Act
            identity = target.FindIdentityById(identityId);
            identity.Value = "Value modified";
            target.ChangeIdentity(identity);

            //Commit
            result = target.FindIdentityById(identityId);
            Assert.IsNotNull(result);
            Assert.AreEqual("Value modified", result.Value);
        }

        /// <summary>
        ///A test for FindIdentityById
        ///</summary>
        [TestMethod()]
        public void FindIdentityById_Invoke_Test()
        {
            //Arrange
            IIdentityManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();
            Guid identityId = Guid.Parse("ABCDF456-1234-5678-5678-EFAB567890AB");
            Identity result = null;

            //Act
            result = target.FindIdentityById(identityId);

            //Commit
            Assert.IsNotNull(result);
            Assert.AreEqual(identityId, result.IdentityId);
        }

        /// <summary>
        ///A test for FindIdentityByUserId
        ///</summary>
        [TestMethod()]
        public void FindIdentityByUserId_Invoke_Test()
        {
            //Arrange
            IIdentityManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();
            Guid identityId = Guid.Parse("ABCDF456-1234-5678-5678-EFAB567890FF");
            List<Identity> result = null;

            //Act
            result = target.FindIdentityByUserId(identityId);

            //Commit
            Assert.IsNotNull(result);
            Assert.AreEqual(identityId, result[0].UserId);
        }

        /// <summary>
        ///A test for FindIdentityTypesByName
        ///</summary>
        [TestMethod()]
        public void FindIdentityTypesByName_Invoke_Test()
        {
            //Arrange
            IIdentityManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();
            string name = "IT";
            List<IdentityType> result = null;

            //Act
            result = target.FindIdentityTypesByName(name);

            //Commit
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
        }

        /// <summary>
        ///A test for RemoveGroupBranch
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RemoveIdentity_Invoke_NullIdentityThrowArgumentNullExceptionTest()
        {
            //Arrange
            IIdentityManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();

            //Act
            target.RemoveIdentity(null);
        }

        /// <summary>
        ///A test for RemoveIdentity
        ///</summary>
        [TestMethod()]
        public void RemoveIdentity_Invoke_Test()
        {
            //Arrange
            IIdentityManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IIdentityManagementService>();
            Guid identityId = Guid.Parse("ABCDF456-1234-5678-5678-AAAFFFFFFFFF");
            Identity identity = new Identity()
            {
                IdentityId = identityId,
                Value = "value"
            };
            Identity result = null;

            //Act
            target.AddIdentity(identity);
            target.RemoveIdentity(identity);

            //Commit
            result = target.FindIdentityById(identityId);
            Assert.IsNull(result);
        }
    }
}
